package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

public class VmetiNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://www.vmeti.com/.*/[0-9]+.html",
		"http://www.vmeti.com/\\?p=[0-9]+"
	};


	
	public boolean isDetailPage(String url) {
		if(url.startsWith("http://www.vmeti.com/zt/"))return false;
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		news.setUrl(url);
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("h1#page_title").text();
		if(StringUtils.isBlank(title)){
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("div.post"));
		date=DateUtils.matchDate(doc.select("div.post_time").text().replace("下午", "").replace("上午", ""));
		news.setAuthor(doc.select("div.post_author:contains(来源:)  span.post_details a ").text());
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setDate(date);

		return news;
	}

	public static void main(String[] args) {
		VmetiNewsAnalyse analyse=new VmetiNewsAnalyse();
		String url="http://www.vmeti.com/case/101632.html";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}

}
